home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / SmallEiffel 0.3.3 / SmallEiffel PPC / lib_std / double.e < prev    next >
Encoding:
Text File  |  1996-06-13  |  4.5 KB  |  231 lines  |  [TEXT/EDIT]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class DOUBLE
  5. --
  6. -- Note : corresponding C type is "double"
  7. --
  8. inherit
  9.    DOUBLE_REF
  10.       redefine
  11.      infix "+", infix "-", infix "*", infix "/", infix "^",
  12.      prefix "+", prefix "-", valid_divisor, infix "<", 
  13.      compare, one, zero, fill_tagged_out_memory
  14.       end;
  15.    
  16. feature {ANY}
  17.  
  18.    infix "+" (other: DOUBLE): DOUBLE is
  19.      -- Add `other' to Current.
  20.       external "CSE"
  21.       end;
  22.  
  23.    infix "-" (other: DOUBLE): DOUBLE is
  24.      -- Subtract `other' from Current.
  25.       external "CSE"
  26.       end;
  27.  
  28.    infix "*" (other: DOUBLE): DOUBLE is
  29.      -- Multiply `other' by Current.
  30.       external "CSE"
  31.       end;
  32.    
  33.    infix "/" (other: DOUBLE): DOUBLE is
  34.      -- Divide Current by `other'.
  35.       external "CSE"
  36.       end;
  37.  
  38.    infix "^" (exp : INTEGER): DOUBLE is
  39.      -- Raise Current to `exp'-th power.
  40.      -- Note: use ANSI C pow.
  41.       external "CSE" 
  42.       end;
  43.  
  44.    prefix "+" : DOUBLE is
  45.       do
  46.      Result := Current
  47.       end;
  48.  
  49.    prefix "-": DOUBLE is
  50.       external "CSE"
  51.       end;
  52.  
  53.    abs: DOUBLE is
  54.       do
  55.      if Current < 0.0 then
  56.         Result := -Current;
  57.      else
  58.         Result := Current;
  59.      end;
  60.       end;
  61.          
  62.    infix "<" (other: DOUBLE): BOOLEAN is
  63.      -- Is Current less than `other'?
  64.       external "CSE"
  65.       end;
  66.    
  67.    compare(other: DOUBLE): INTEGER is
  68.      -- Compare Current with `other'.
  69.      -- '<' <==> Result < 0
  70.      -- '>' <==> Result > 0
  71.      -- Otherwise Result = 0
  72.       do
  73.      if Current < other then
  74.         Result := -1
  75.      else
  76.         if Current > other then
  77.            Result := 1
  78.         end;
  79.      end;
  80.       end;
  81.  
  82.    valid_divisor(other: DOUBLE): BOOLEAN is
  83.       do
  84.      Result := (other /= 0.0)
  85.       end;
  86.    
  87.    one: DOUBLE is 1.0;
  88.    
  89.    zero: DOUBLE is 0.0;
  90.    
  91.    floor: INTEGER is
  92.      -- Greatest integral value no greater than Current.
  93.       external "CSE"
  94.       ensure 
  95.      result_no_greater: Result <= Current;
  96.      close_enough: Current - Result < one;
  97.       end;
  98.       
  99.    ceiling: INTEGER is
  100.      -- Smallest integral value no smaller than Current.
  101.       do
  102.      Result := floor + 1;
  103.       ensure
  104.      result_no_smaller: Result >= Current;
  105.      -- *** BUG in ELKS :
  106.      -- close_enough: Result - Current < one;
  107.       end;
  108.    
  109.    truncated_to_integer: INTEGER is
  110.      -- Integer part (same sign, largest absolute value
  111.      -- no greater than Current).
  112.       local
  113.      
  114.       do
  115.      Result := floor;
  116.      if 0.5 + Result < Current then
  117.         Result := ceiling;
  118.      end;
  119.       end;
  120.    
  121.    to_real: REAL is
  122.      -- Note: C conversion from "double" to "float".
  123.      -- Thus, Result can be undefine (ANSI C).
  124.       external "CSE"
  125.       end;
  126.    
  127.    to_string: STRING is
  128.      -- Convert the DOUBLE into a new allocated STRING. 
  129.      -- Note: see `append_in' to save memory.
  130.       do
  131.      !!Result.make(0);
  132.      append_in(Result);
  133.       end; 
  134.  
  135.    append_in(str: STRING) is
  136.      -- Append the equivalent of `to_string' at the end of 
  137.      -- `str'. Thus you can save memory because no other
  138.      -- STRING is allocate for the job.
  139.       require
  140.      str /= Void;
  141.       local
  142.      d: DOUBLE;
  143.      i: INTEGER;
  144.       do
  145.      if Current < 0 then
  146.         str.extend('-');
  147.         d := -Current;
  148.      else
  149.         d := Current;
  150.      end;
  151.      i := d.floor;
  152.      i.append_in(str);
  153.      d := d - i;
  154.      if d /= 0.0 then
  155.         from
  156.            str.extend('.');
  157.         invariant
  158.            d < 1;
  159.         until
  160.            d = 0.0
  161.         loop
  162.            d := d * 10.0;
  163.            i := d.floor;
  164.            d := d - i;
  165.            str.extend(i.digit);
  166.         end;
  167.      end;
  168.       end; 
  169.    
  170.    to_string_format(d: INTEGER): STRING is
  171.      -- Convert the DOUBLE into a new allocated STRING including 
  172.      -- only `d' digits in fractionnal part. 
  173.      -- Note: see `append_in_format' to save memory.
  174.       do
  175.      !!Result.make(0);
  176.      append_in_format(Result,d);
  177.       end; 
  178.  
  179.    append_in_format(str: STRING; f: INTEGER) is
  180.      -- Same as `append_in' but produce only `f' digit of 
  181.      -- the fractionnal part.
  182.       require
  183.      str /= Void;
  184.      f >= 0;
  185.       local
  186.      r: DOUBLE;
  187.      i, f_count: INTEGER;
  188.       do
  189.      if Current < 0 then
  190.         str.extend('-');
  191.         r := -Current;
  192.      else
  193.         r := Current;
  194.      end;
  195.      i := r.floor;
  196.      i.append_in(str);
  197.      str.extend('.');
  198.      r := r - i;
  199.      from
  200.         f_count := f;
  201.      invariant
  202.            r < 1;
  203.      until
  204.         f_count = 0
  205.      loop
  206.         r := r * 10.0;
  207.         i := r.floor;
  208.         r := r - i;
  209.         str.extend(i.digit);
  210.         f_count := f_count - 1;
  211.      end;
  212.       end; 
  213.    
  214.    sqrt: DOUBLE is
  215.      -- Compute the square routine.
  216.      -- Note: use ANSI C sqrt.
  217.       require
  218.      Current >= 0;
  219.       external "CSE"
  220.       end;
  221.    
  222. feature -- Object Printing :
  223.  
  224.    fill_tagged_out_memory is
  225.       do
  226.      Current.append_in(tagged_out_memory);
  227.       end;
  228.  
  229. end -- DOUBLE
  230.  
  231.